package com.etrans.web.gps.business.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.system.domain.SysUser;
import com.etrans.web.gps.business.system.service.CommonService;
import com.etrans.web.gps.common.Constant;
import com.etrans.web.gps.util.TreeCommon;

@Controller
@RequestMapping("/system/common/")
public class CommonController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(CommonController.class);
	
	@Autowired
	private CommonService commonService;
	
	// 查询用户下的接入平台
		@RequestMapping(value = "queryAllPlatform.html")
		@ResponseBody
		public List<HashMap<String, Object>> queryAllPlatform() {
			List<HashMap<String, Object>> list = null;
			HashMap<String, Object> param = new HashMap<>();
			SysUser user=getSysUser();
			param.put("FullID", String.format("%08d", user.getArea_id()));
			try {
				list = commonService.queryAllPlatform(param);
			} catch (Exception e) {
				logger.error("", e);
			}
			return list;
		}

		@RequestMapping(value = "queryAllTrade.html")
		@ResponseBody
		public List<HashMap<String, Object>> queryAllTrade() {
			List<HashMap<String, Object>> list = null;
			HashMap<String, Object> param = new HashMap<>();
			SysUser user=getSysUser();
			param.put("FullID", String.format("%08d", user.getArea_id()));
			try {
				list = commonService.queryAllTrade(param);
			} catch (Exception e) {
				logger.error("", e);
			}
			return list;
		}

		@RequestMapping(value = "queryAreaTree.html")
		@ResponseBody
		public List<Map<String, Object>> queryAreaTree() {
			HashMap<String, Object> param = new HashMap<>();
			SysUser user=getSysUser();
			param.put("FullID", String.format("%08d", user.getArea_id()));
			List<HashMap<String, Object>> listTreeList = commonService.queryAreaTree(param);
			int level = 0;
			if (listTreeList.size() > 0) {
				level = listTreeList.get(0).get("FullID").toString().length();
			}
			 
			return TreeCommon.createTree(listTreeList, level);
		}
		
		@RequestMapping(value = "queryVehicleTypeTree.html")
		@ResponseBody
		public List<Map<String, Object>> queryVehicleTypeTree() {
			HashMap<String, Object> param = new HashMap<>();
			
			List<HashMap<String, Object>> treeList = commonService.queryVehicleTypeTree(param);
			int level = 0;
			if (treeList.size() > 0) {
				level = treeList.get(0).get("FullID").toString().length();
			}
			return TreeCommon.createTree(treeList,level);
		}
		
		@RequestMapping(value = "queryOrganzationTree.html")
		@ResponseBody
		public List<Map<String, Object>> queryOrganzationTree() {
			HashMap<String, Object> param = new HashMap<>();
			List<HashMap<String, Object>> treeList = commonService.queryOrganzationTree(param);
			int level = 0;
			if (treeList.size() > 0) {
				level = treeList.get(0).get("FullID").toString().length();
			}
			return TreeCommon.createTree(treeList,level);
		}
		
		@RequestMapping(value = "queryTeminalType.html")
		@ResponseBody
		public List<HashMap<String, Object>> queryTeminalType() {
			List<HashMap<String, Object>> list = null;
			HashMap<String, Object> param = new HashMap<>();
			try {
				list = commonService.queryTeminalType(param);
			} catch (Exception e) {
				logger.error("", e);
			}
			return list;
		}
		
		
		@RequestMapping(value = "queryEnterprise.html")
		@ResponseBody
		public List<HashMap<String, Object>> queryEnterpise(HttpServletRequest request) {
			String name = request.getParameter("name");
			String pageSize = request.getParameter("pageSize");
			HashMap<String, Object> param = new HashMap<>();
			SysUser user=getSysUser();
			param.put("FullID", String.format("%08d", user.getArea_id()));
			param.put("name", name);
			if (null == pageSize) {
				param.put("selected", "selected");
			}
			
			return commonService.queryEnterprise(param);
		}
		
		@RequestMapping(value = "queryBusinessScopeTree.html")
		@ResponseBody
		public List<Map<String, Object>> queryBusinessScopeTree() {
			HashMap<String, Object> param = new HashMap<>();
			List<HashMap<String, Object>> treeList = commonService.queryBusinessScopeTree(param);
			int level = 0;
			if (treeList.size() > 0) {
				level = treeList.get(0).get("FullID").toString().length();
			}
			return TreeCommon.createTree(treeList,level);
		}
		
		
		/**
		 * 查岗内容下拉
		 * @return
		 */
		@RequestMapping(value = "queryQuestions.html")
		@ResponseBody
		public List<HashMap<String, Object>> queryQuestions() {
			List<HashMap<String, Object>> list = null;
			HashMap<String, Object> param = new HashMap<>();
			try {
				list = commonService.queryQuestions(param);
			} catch (Exception e) {
				logger.error("", e);
			}
			return list;
		}
		
		/**
		 * 业户查岗下拉
		 * @return
		 */
		@RequestMapping(value = "selectEnterprise.html")
		@ResponseBody
		public List<HashMap<String, Object>> selectEnterprise() {
			List<HashMap<String, Object>> list = null;
			HashMap<String, Object> param = new HashMap<>();
			SysUser user=getSysUser();
			param.put("FullID", String.format("%08d", user.getArea_id()));
			try {
				list = commonService.selectEnterprise(param);
			} catch (Exception e) {
				logger.error("", e);
			}
			return list;
			
		}
		
		/**
		 * 区域划分下拉
		 * @return
		 */
		@RequestMapping("queryLevel.html")
		@ResponseBody
		public List<HashMap<String, Object>> queryLevel(){
			Integer areaId = this.getSysUser().getArea_id();
			List<HashMap<String, Object>> list=new ArrayList<HashMap<String, Object>>();
			HashMap<String, Object> where1 = new HashMap<String, Object>();
			HashMap<String, Object> where2 = new HashMap<String, Object>();
			HashMap<String, Object> where3 = new HashMap<String, Object>();
			Integer area_level = commonService.findAreaLevelById(areaId);
			if(area_level==1){
				where1.put("id", 1);
				where1.put("text", "省级");
				list.add(where1);
			}
			if(area_level!=3){
				where2.put("id", 2);
				where2.put("text", "市级");
				list.add(where2);
			}
			where3.put("id", 3);
			where3.put("text", "区县级");
			list.add(where3);
			return list;
		}
		
		
		/**
		 * 区域树形
		 * @param level
		 * @return
		 */
		@RequestMapping("queryAreaName.html")
		@ResponseBody
		public List<Map<String, Object>> queryAreaName(String level){
			SysUser user = this.getSysUser();
			HashMap<String, Object> param = new HashMap<String, Object>();
			List<HashMap<String, Object>> list=new ArrayList<>();
			List<HashMap<String, Object>> listTreeList=new ArrayList<>();
			Integer area_level = commonService.findAreaLevelById(user.getArea_id());
			if(area_level==1 && level.equals("1")){
				param.put("areaId",user.getArea_id());
				listTreeList=commonService.findAreaName(param);
			}else if(area_level==1 && level.equals("2")){
				param.put("FullID", user.getFullID());
				list=commonService.queryAreaTree(param);
				for (HashMap<String, Object> hashMap : list) {
					if(hashMap.get("Level").equals(1) || hashMap.get("Level").equals(2)){
						listTreeList.add(hashMap);
					}
				}
			}else if(area_level==2 && level.equals("2")){
				param.put("areaId",user.getArea_id());
				listTreeList=commonService.findAreaName(param);
			}else{
				param.put("FullID", user.getFullID());
				listTreeList=commonService.queryAreaTree(param);
			}
			int i = 0;
			if (listTreeList.size() > 0) {
				i = listTreeList.get(0).get("FullID").toString().length();
			}
			 
			return TreeCommon.createTree(listTreeList, i);
		}

	    @RequestMapping("selectVehicle.html")
	    public void loadUserVehicle(HttpServletResponse response,HttpServletRequest request){
	    	String name = request.getParameter("name");
 	    	SysUser user = getSysUser();
	    	HashMap<String,Object> param = new HashMap<String, Object>();
			param.put("fullID", user.getFullID());
			param.put("selected", "selected");
			param.put("vehicle_no", name);
			if(name != null && name.contains("_")){
				param.put("vehicle_no", name.split("_")[0]);
				param.put("vehicle_color", name.split("_")[1]);
			}
	    	
	    	List<HashMap<String,Object>> list = commonService.queryVeListByRole(param);
	    	this.ajaxResultJson(response, list);
	    }
	    
	    /**
	     * 查询用户所在区域下的业户
	     * @return
	     * @throws Exception
	     */
	    
	    @RequestMapping(value = "loadUserUnit.html")
	    @ResponseBody
	    public List<HashMap<String, Object>> loadUserUnit() throws Exception {
	        SysUser user = (SysUser) getSession().getAttribute(Constant.LOGIN_SESSION_NAME);
	        HashMap<String,Object> param=getReqParameters();
			String unitName=null;
			if(param.get("unitName")!=null){
				unitName=param.get("unitName").toString();
			}
			List<HashMap<String, Object>> ent_List = commonService.queryEnterAndPlatByRoleNotLimit(user.getArea_id(),unitName);
	        return ent_List;
	    }
	    
	    /**
	     * 查询业户下的车辆
	     * @return
	     * @throws Exception
	     */   
	    @RequestMapping(value = "loadUserVehicle.html")
	    @ResponseBody
	    public List<HashMap<String, Object>> loadVehicle() throws Exception {
	        SysUser user = (SysUser) getSession().getAttribute(Constant.LOGIN_SESSION_NAME);
	        HashMap<String,Object> param=getReqParameters(); 
	        if(param.get("areaID")!=null){
	        	 param.put("fullID", String.format("%08d",param.get("areaID").toString()));  
	        }
	            	
	    	List<HashMap<String,Object>> vehicles_list = commonService.queryVeListByRole(param);
	        return vehicles_list;
	    }
	    
}
